import {
  Body,
  Controller,
  Delete,
  Get,
  Headers,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { DictTypeService } from './dict_type.service';
import { createData, deleteType, getList, updateData } from './dict_type.dto';
import { Result } from 'src/result/Result';

@Controller('/dict_type')
export class DictTypeController {
  constructor(private readonly dictTypeService: DictTypeService) {}

  // 新增字典类型
  @Post()
  async create(
    @Body() args: createData,
    @Headers('authorization') token: string,
  ) {
    const count = await this.dictTypeService.createDictType(args, token);
    return Result.toAjax(count);
  }

  // 修改字典类型
  @Put()
  async update(
    @Body() args: updateData,
    @Headers('authorization') token: string,
  ) {
    const count = await this.dictTypeService.updateDictType(args, token);
    return Result.toAjax(count);
  }

  // 查询字典类型列表
  @Get('/list')
  async finAll(@Query() { pageNum, pageSize }: getList) {
    const rows = await this.dictTypeService.findAll({ pageNum, pageSize });
    return Result.ok(rows);
  }

  // 获取字典选择框列表
  @Get('/optionselect')
  async optionselect() {
    const rows = await this.dictTypeService.optionselect();
    return Result.ok(rows);
  }

  // 查询字典类型详细
  @Get(':dictId')
  async getDictType(@Param('dictId') dictId: number) {
    const data = await this.dictTypeService.getDictType(+dictId);
    return Result.ok(data);
  }

  //刷新字典缓存
  @Delete('/refreshCache')
  async refreshCache() {
    const count = await this.dictTypeService.refreshCache();
    return Result.toAjax(count);
  }

  // 删除字典类型
  @Delete('/:dictIds')
  async deleteDictType(@Param() { dictIds }: deleteType) {
    const count = await this.dictTypeService.deleteDictType({ dictIds });
    return Result.toAjax(count);
  }
}
